import { EntitySubscriberInterface, EventSubscriber, InsertEvent, UpdateEvent } from 'typeorm';

@EventSubscriber()
export class AuditSubscriber implements EntitySubscriberInterface {
  constructor() {}

  beforeInsert(event: InsertEvent<any>): Promise<any> | void {
    const entity = event.entity;
    // entity.createBy = 'admin';
  }

  beforeUpdate(event: UpdateEvent<any>): Promise<any> | void {}

  isSubclassOf(childClass, parentClass) {
    // 获取子类实例的原型的原型（即父类的原型）
    let currentProto = Object.getPrototypeOf(childClass.prototype);

    while (currentProto !== null) {
      // 如果找到父类的原型，返回 true
      if (currentProto === parentClass.prototype) {
        return true;
      }
      // 继续向上遍历原型链
      currentProto = Object.getPrototypeOf(currentProto);
    }

    // 遍历结束未找到，返回 false
    return false;
  }
}
